package com.gamebox.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import com.gamebox.dao.GameDao;
import com.gamebox.dao.UserDao;
import com.gamebox.javaBean.User;

public class LoginFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3367867969975477065L;
	private User loginUser;
	private UserDao userDao = new UserDao();
	private GameDao gameDao = new GameDao();

	private JPanel loginJPanel;
	private JLabel welcomeJLabel;
	private JLabel userNameJLabel;
	private JTextField userNameField;
	private JLabel passwordJLabel;
	private JTextField passwordField;
	private JButton loginButton;
	private JButton registerButton;

	private JPanel registerJPanel;
	private JLabel registerNameJLabel;
	private JTextField registerNameField;
	private JLabel registerPassJLabel;
	private JPasswordField registerPassField;
	private JLabel registerNickJLabel;
	private JTextField registerNickField;
	private JButton registerConfirmButton;

	public LoginFrame() {
		setFrame();

		composeLoginPanel();

		composeRegisterPanel();

		gotoLogin();
	}

	private void setFrame() {
		this.setTitle("游戏盒");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(400, 400);
		this.setLocation(
				this.getToolkit().getScreenSize().width / 2 - this.getWidth()
						/ 2, this.getToolkit().getScreenSize().height / 2
						- this.getHeight() / 2);

		this.setLayout(null);
		this.setResizable(false);
		this.setVisible(true);
	}

	private void composeLoginPanel() {
		loginJPanel = new JPanel();
		loginJPanel.setLayout(null);
		welcomeJLabel = new JLabel("欢迎登陆游戏盒应用");
		welcomeJLabel.setBounds(50, 50, 300, 30);
		loginJPanel.add(welcomeJLabel);

		userNameJLabel = new JLabel("用户名：");
		userNameJLabel.setBounds(50, 100, 100, 30);
		loginJPanel.add(userNameJLabel);

		userNameField = new JTextField();
		userNameField.setBounds(160, 100, 200, 30);
		loginJPanel.add(userNameField);

		passwordJLabel = new JLabel("密码：");
		passwordJLabel.setBounds(50, 150, 100, 30);
		loginJPanel.add(passwordJLabel);

		passwordField = new JPasswordField();
		passwordField.setBounds(160, 150, 200, 30);
		loginJPanel.add(passwordField);

		loginButton = new JButton("登陆");
		loginButton.setBounds(200, 200, 80, 30);
		loginJPanel.add(loginButton);
		loginButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String name = userNameField.getText();
				String password = passwordField.getText();
				if (name.equals("") || password.equals("")) {
					JOptionPane.showMessageDialog(null, "用户名密码不能为空");
				} else {
					// User user = findUser(name, password, users);
					if (name.equals("root") && password.equals("root")) {
						final User user = userDao.login(name, password);
						if (user == null) {
							JOptionPane.showMessageDialog(null, "用户名或密码错误！");

						} else {
							final UserTable userTable = new UserTable(userDao
									.getAllUsers());
							JTable t = userTable.getTable();
							JScrollPane jScrollPane = new JScrollPane(t);
							final AdminGameTable agt = new AdminGameTable(
									gameDao.getAllGames());
							JTable at = agt.getTable();
							JScrollPane ajScrollPane = new JScrollPane(at);
							JFrame userFrame = new JFrame("管理员界面");
							JTabbedPane jTabbedpane = new JTabbedPane();
							JPanel userPanel = new JPanel();
							userPanel.add(jScrollPane);
							// JButton ub1 = new JButton("按玩过游戏数排序");
							// ub1.addActionListener(new ActionListener() {
							//
							// @Override
							// public void actionPerformed(ActionEvent arg0) {
							// // TODO Auto-generated method stub
							//
							// }
							// });
							// userPanel.add(ub1);
							JButton ub2 = new JButton("按序号排序");
							ub2.addActionListener(new ActionListener() {

								@Override
								public void actionPerformed(ActionEvent e) {
									// TODO Auto-generated method stub
									userTable.update(userDao.getAllUsersById());
									userTable.fireTableDataChanged();
								}
							});
							userPanel.add(ub2);
							JButton ub3 = new JButton("按用户名排序");
							ub3.addActionListener(new ActionListener() {

								@Override
								public void actionPerformed(ActionEvent e) {
									// TODO Auto-generated method stub
									userTable.update(userDao
											.getAllUsersByName());
									userTable.fireTableDataChanged();
								}
							});
							userPanel.add(ub3);
							jTabbedpane.addTab("用户", userPanel);
							JPanel gamePanel = new JPanel();
							gamePanel.add(ajScrollPane);
							JButton addBt = new JButton("加入我的游戏列表");
							gamePanel.add(addBt);
							JButton rerank1 = new JButton("按点击量排序");
							rerank1.addActionListener(new ActionListener() {

								@Override
								public void actionPerformed(ActionEvent arg0) {
									// TODO Auto-generated method stub
									agt.setGames(gameDao
											.getAllGamesByClickNum());
									agt.fireTableDataChanged();
								}
							});
							gamePanel.add(rerank1);
							JButton rerank2 = new JButton("按得分排序");
							rerank2.addActionListener(new ActionListener() {

								@Override
								public void actionPerformed(ActionEvent arg0) {
									// TODO Auto-generated method stub
									agt.setGames(gameDao.getAllGamesByScore());
									agt.fireTableDataChanged();
								}
							});
							gamePanel.add(rerank2);
							JButton rerank3 = new JButton("按玩家数排序");
							gamePanel.add(rerank3);
							rerank3.addActionListener(new ActionListener() {

								@Override
								public void actionPerformed(ActionEvent arg0) {
									// TODO Auto-generated method stub
									agt.setGames(gameDao
											.getAllGamesByPlayNum());
									agt.fireTableDataChanged();
								}
							});
							jTabbedpane.addTab("游戏", gamePanel);
							final GameTable gt = new GameTable(gameDao
									.getAllGames(user));
							JTable atb = gt.getTable();
							JScrollPane scrollPane = new JScrollPane(atb);
							JPanel mygamePanel = new JPanel();
							mygamePanel.add(scrollPane);
							jTabbedpane.addTab("我的游戏", mygamePanel);
							addBt.addActionListener(new ActionListener() {

								@Override
								public void actionPerformed(ActionEvent arg0) {
									// TODO Auto-generated method stub
									int i = agt.getTable().getSelectedRow();
									if(i>=0){
									String name = (String) agt.getValueAt(i, 0);
									gameDao.addSaveRecord(user.getId(), gameDao
											.getGamesByName(name).get(0)
											.getId());
									gt.setGames(gameDao
											.getAllGames(user));
									gt.fireTableDataChanged();
									JOptionPane.showMessageDialog(null, "添加成功");
									}
								}
							});
							userFrame.setBounds(400, 150, 550, 550);
							userFrame.setVisible(true);
							userFrame
									.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
							userFrame.getContentPane().add(jTabbedpane,
									BorderLayout.CENTER);
							userFrame.repaint();
							userFrame.validate();
							LoginFrame.this.hide();
						}
					} else {
						User user = userDao.login(name, password);
						if (user == null) {
							JOptionPane.showMessageDialog(null, "用户名或密码错误！");

						} else {
							loginUser = user;
							GameBoxFrame gameBoxFrame = new GameBoxFrame(
									loginUser);
							gameBoxFrame.setVisible(true);
							LoginFrame.this.hide();
						}
					}

				}
			}
		});

		registerButton = new JButton("注册");
		registerButton.setBounds(300, 200, 80, 30);
		loginJPanel.add(registerButton);
		registerButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				gotoRegister();
			}
		});

		loginJPanel.setBounds(0, 0, 400, 400);
	}

	private void composeRegisterPanel() {
		registerJPanel = new JPanel();
		registerJPanel.setBounds(0, 0, 400, 400);
		registerJPanel.setLayout(null);
		registerNameJLabel = new JLabel("用户名:");
		registerNameJLabel.setBounds(100, 100, 100, 30);
		registerJPanel.add(registerNameJLabel);

		registerNameField = new JTextField();
		registerNameField.setBounds(220, 100, 150, 30);
		registerJPanel.add(registerNameField);

		registerPassJLabel = new JLabel("密码：");
		registerPassJLabel.setBounds(100, 150, 100, 30);
		registerJPanel.add(registerPassJLabel);

		registerPassField = new JPasswordField();
		registerPassField.setBounds(220, 150, 150, 30);
		registerJPanel.add(registerPassField);

		registerNickJLabel = new JLabel("昵称:");
		registerNickJLabel.setBounds(100, 200, 100, 30);
		registerJPanel.add(registerNickJLabel);

		registerNickField = new JTextField();
		registerNickField.setBounds(220, 200, 150, 30);
		registerJPanel.add(registerNickField);

		registerConfirmButton = new JButton("注册");
		registerConfirmButton.setBounds(300, 250, 80, 30);
		registerJPanel.add(registerConfirmButton);
		registerConfirmButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String userName = registerNameField.getText();
				String password = registerPassField.getText();
				String nickName = registerNickField.getText();
				// User user = findUser//Name(userName, users);
				User user = userDao.getUserByName(userName);
				if (user != null) {
					JOptionPane.showMessageDialog(null, "用户名已存在！");
				} else {
					if (userName.equals("") || password.equals("")) {
						JOptionPane.showMessageDialog(null, "用户名或密码为空！");
					} else {
						user = new User(0, userName, password, 1, nickName, 0,
								new Date());
						userDao.addUser(user);
						gotoLogin();
					}
				}
			}
		});
	}

	/**
	 * 到登陆界面
	 */
	public void gotoLogin() {
		this.removePanel();
		this.add(loginJPanel);
		this.repaint();
		this.invalidate();
	}

	private void removePanel() {
		this.remove(registerJPanel);
		this.remove(loginJPanel);
	}

	/**
	 * 到注册界面
	 */
	public void gotoRegister() {
		this.removePanel();
		this.add(registerJPanel);
		this.repaint();
		this.invalidate();
	}

}
